import { createRouter, createWebHashHistory, createWebHistory } from 'vue-router'

import { successDeal, getStore, removeStore } from '@/utils/utils'

const routes = [
  {
    path: '/',
    name: 'login',
    component: () => import('@/views/login/index.vue'),
  },
  {
    path: '/layout',
    name: 'layout',
    redirect: '/home',
    component: () => import('@/views/layout/index.vue'),
    meta: {
      title: '自然人代开',
    },
    children: [
      {
        path: '/userMangment',
        name: 'userMangment',
        component: () => import('@/views/userMangment/index.vue'),
        meta: {
          title: '用户管理',
          parentPath: '/userMangment',
          defaultActive: '/userMangment',
          hidden: true,
          icon: 'UserFilled',
        },
      },
      {
        path: '/userMangment/add',
        name: 'userMangmentAdd',
        component: () => import('@/views/userMangment/add.vue'),
        meta: {
          title: '新增用户',
          parentPath: '/userMangment',
          defaultActive: '/userMangment',
          hidden: false,
        },
      },
      {
        path: '/userMangment/edit',
        name: 'userMangmentEdit',
        component: () => import('@/views/userMangment/edit.vue'),
        meta: {
          title: '修改用户',
          parentPath: '/userMangment',
          defaultActive: '/userMangment',
          hidden: false,
        },
      },
      {
        path: '/otherConfiguration',
        name: 'otherConfiguration',
        component: () => import('@/views/otherConfiguration/index.vue'),
        meta: {
          title: '其他配置',
          parentPath: '/otherConfiguration',
          defaultActive: '/otherConfiguration',
          hidden: true,
          icon: 'Management',
        },
        children: [
          {
            path: '/problem',
            name: 'problem',
            component: () => import('@/views/otherConfiguration/problem/index.vue'),
            meta: {
              title: '常见问题',
              parentPath: '/otherConfiguration',
              defaultActive: '/problem',
              hidden: true,
            },
          },
          {
            path: '/problem/add',
            name: 'problemAdd',
            component: () => import('@/views/otherConfiguration/problem/add.vue'),
            meta: {
              title: '新增常见问题',
              parentPath: '/otherConfiguration',
              defaultActive: '/problem',
              hidden: false,
            },
          },
          {
            path: '/problem/edit',
            name: 'problemEdit',
            component: () => import('@/views/otherConfiguration/problem/edit.vue'),
            meta: {
              title: '编辑常见问题',
              parentPath: '/otherConfiguration',
              defaultActive: '/problem',
              hidden: false,
            },
          },

          {
            path: '/helpCenter',
            name: 'helpCenter',
            component: () => import('@/views/otherConfiguration/helpCenter/index.vue'),
            meta: {
              title: '帮助中你',
              parentPath: '/otherConfiguration',
              defaultActive: '/helpCenter',
              hidden: true,
            },
          },
          {
            path: '/helpCenter/add',
            name: 'helpCenterAdd',
            component: () => import('@/views/otherConfiguration/helpCenter/add.vue'),
            meta: {
              title: '新增帮助中你',
              parentPath: '/otherConfiguration',
              defaultActive: '/helpCenter',
              hidden: false,
            },
          },
          {
            path: '/helpCenter/edit',
            name: 'helpCenterEdit',
            component: () => import('@/views/otherConfiguration/helpCenter/edit.vue'),
            meta: {
              title: '编辑帮助中你',
              parentPath: '/otherConfiguration',
              defaultActive: '/helpCenter',
              hidden: false,
            },
          },
        ]
      }
    ],
  },
]

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes,
})

// 免登录的白名单
let whiteList = ['/']

router.beforeEach((to, from, next) => {


  let token = getStore('YX_BACK_ZRRDK_USERINFO')?.token || ''


  if (!token && whiteList.indexOf(to.path) === -1) {
    next({ path: '/' })
    return false
  }

  next()
})

router.afterEach((to, from) => { })

export default {
  router,
  routes,
}
